package com.usefullc.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.system.domain.DataDict;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.query.DataDictQuery;
import com.usefullc.system.service.DataDictService;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.system.vo.DataDictTreeVo;
import com.usefullc.system.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Sampson
 * @date: 2022-04-15 15:28
 */
@RestController
@RequestMapping(path = "/system/dataDict/")
@Api( tags="数据字典管理")
public class DataDictController extends BaseController {

    @Autowired
    private DataDictService dataDictService;


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取列表", response = WebResult.class)
    @GetMapping(path = "list", produces = "application/json")
    public WebResult list(DataDictQuery query) {
        Pager<DataDictTreeVo> pager = dataDictService.listByPage(query);
        return WebResultBuilder.successResult(pager);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取树列表", response = WebResult.class)
    @GetMapping(path = "listTree", produces = "application/json")
    public WebResult listTree(DataDictQuery query) {
        List<TreeVo> dataList = dataDictService.listTree(query);
        return WebResultBuilder.successResult(dataList);

    }


    @ApiOperation(value = "保存字典", response = WebResult.class)
    @PostMapping(path = "save", produces = "application/json")
    public WebResult save(@UsefulValid @RequestBody DataDict dataDict) {
        dataDictService.saveDataDict(dataDict);
        return WebResultBuilder.successResult(dataDict);
    }

    @ApiOperation(value = "修改字典", response = WebResult.class)
    @PostMapping(path = "update", produces = "application/json")
    public WebResult update(@UsefulValid(update = true)  @RequestBody DataDict dataDict) {
        dataDict = dataDictService.updateDataDict(dataDict);
        return WebResultBuilder.successResult(dataDict);
    }


    @ApiOperation(value = "删除字典", response = WebResult.class)
    @PostMapping(path = "del", produces = "application/json")
    public WebResult del(@UsefulValid @RequestBody IdsParam idsParam) {
        dataDictService.deletePhysByIds(idsParam.getIds());
        return WebResultBuilder.successResult();
    }
}
